package com.barunsw.bwf.dhtmlx_sample.tutorial1;

import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/tutorial1")
public class DhtmlxTutorial1Controller {
	@Autowired
	DhtmlxTutorial1Service dhtmlxTutorial1Service;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String tutorial1() {
		return "dhtmlx_tutorial/tutorial1";
	}
	
	@RequestMapping(value="/contacts", method=RequestMethod.GET)
	public @ResponseBody List<DhtmlxTutorial1ContactVo> getContactList() throws Exception {
		List<DhtmlxTutorial1ContactVo> contactList = dhtmlxTutorial1Service.selectList();
		return contactList;
	}
	
	@RequestMapping(value="/contacts", method=RequestMethod.POST)
	public @ResponseBody DhtmlxTutorial1ResultVo insertContact(@RequestBody DhtmlxTutorial1ContactVo contactVo) throws Exception {
		DhtmlxTutorial1ResultVo resultVo = new DhtmlxTutorial1ResultVo(DhtmlxTutorial1Result.OK, "");
		
		int result = dhtmlxTutorial1Service.insert(contactVo);
		if ( result <= 0 ) {
			resultVo.setResult(DhtmlxTutorial1Result.NOK);
			resultVo.setReason("Contact 에러.");
		}
		return resultVo;
	}
}
